//
//  SMSaveMoneyListViewController.m
//  SaveMoney
//
//  Created by user on 2018/11/26.
//  Copyright © 2018年 Sunflower. All rights reserved.
//

#import "SMSaveMoneyListViewController.h"

#import "UIAlertController+Blocks.h"
#import "SMSaveMoneyListTableViewCell.h"
#import "NSAttributedString+YYText.h"

@interface SMSaveMoneyListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, copy) NSMutableArray *listArray;
@property (weak, nonatomic) IBOutlet UIView *maskView;

@end

@implementation SMSaveMoneyListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _listArray = [[NSMutableArray alloc] initWithArray:[SMMoneyModel findAll]];
    if (_listArray.count == 0) {
        [_maskView setHidden:NO];
    } else {
        [_maskView setHidden:YES];
    }
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SBScaleNumber(150);
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SMSaveMoneyListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMSaveMoneyListTableViewCell" forIndexPath:indexPath];
    
    SMMoneyModel *model = _listArray[indexPath.row];
    [cell.image setImage:[UIImage base64ChangeImageWithEncode:model.backImage]];
    cell.bigTitleLabel.text = model.saveMoneyTitle;
    
    NSMutableAttributedString *total =  [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"目标金额：￥%ld",(long)model.totalMoney]];
    [total yy_setFont:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 5)];
    [total yy_setColor:[UIColor redColor] range:NSMakeRange(5, total.length-5)];
    cell.totalLabel.attributedText = total;
    
    NSMutableAttributedString *alrMoney = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"已存：￥%ld",(long)model.alreadyMoney]];
    [alrMoney yy_setFont:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 3)];
    [alrMoney yy_setColor:[UIColor redColor] range:NSMakeRange(3, alrMoney.length-3)];
    cell.arlLabel.attributedText = alrMoney;
    
    cell.timeLabel.text = [NSString stringWithFormat:@"%@", model.creationTime];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        SMMoneyModel *model = _listArray[indexPath.row];
        __weak typeof(self) weakSelf = self;
        [UIAlertController showAlertInViewController:self withTitle:@"是否删除此计划任务" message:model.saveMoneyTitle cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil tapBlock:^(UIAlertController * _Nonnull controller, UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
            if (action.style != UIAlertActionStyleCancel) {
                [SVProgressHUD show];
                NSArray *array = [SMSaveMoneyMessageModel findByCriteria:[NSString stringWithFormat:@"Where userKey = '%@' And projectId = %d",model.userKey,model.id]];
                [SMSaveMoneyMessageModel deleteObjects:array];
                [model deleteObject];
                [weakSelf.listArray removeObject:model];
                [SVProgressHUD dismiss];
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
                
            }
        }];
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"删除";
}


- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
